//
//  KDDayTableViewCell.m
//  KongDays
//
//  Created by deeplu on 13. 1. 3..
//  Copyright (c) 2013년 BlueBeans. All rights reserved.
//

#import "KDDayTableViewCell.h"
#import "NSDay.h"

@protocol KDDayTableViewCell

- (void)pictureButtonTapped;

@end

@implementation KDDayTableViewCell
@synthesize day = day_;
@synthesize pictureButton;
@synthesize titleLabel;
@synthesize dateLabel;
@synthesize countLabel;
@synthesize delegate;

- (void)dealloc
{
    [self.day release];
    [self.pictureButton release];
    [self.titleLabel release];
    [self.dateLabel release];
    [self.countLabel release];
    
    [super dealloc];
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.pictureButton = [[UIButton alloc] initWithFrame:CGRectZero];
        [self.pictureButton addTarget:self
                          action:@selector(pictureButtonTapped)
                forControlEvents:UIControlEventTouchUpInside];
        
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.titleLabel.backgroundColor = [UIColor clearColor];
        self.titleLabel.font = [UIFont boldSystemFontOfSize:16.0f];

        self.dateLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.dateLabel.backgroundColor = [UIColor clearColor];
        self.dateLabel.font = [UIFont boldSystemFontOfSize:10.0f];
        self.dateLabel.textColor = [UIColor grayColor];

        self.countLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.countLabel.backgroundColor = [UIColor clearColor];
        self.countLabel.font = [UIFont boldSystemFontOfSize:30.0f];
        self.countLabel.textColor = [UIColor lightGrayColor];
        self.countLabel.textAlignment = NSTextAlignmentRight;
        
        [self.contentView addSubview:pictureButton];
        [self.contentView addSubview:titleLabel];
        [self.contentView addSubview:dateLabel];
        [self.contentView addSubview:countLabel];

    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    if (self.pictureButton) {
        self.pictureButton.frame = CGRectMake(10.0, 7.0, 30, 30);
        [self.contentView bringSubviewToFront:pictureButton];
    }
    
    if (self.titleLabel) {
        self.titleLabel.frame = CGRectMake(45.0, 5.0, 155.0, 20.0);
        [self.contentView bringSubviewToFront:titleLabel];
    }
    
    if (self.dateLabel) {
        self.dateLabel.frame = CGRectMake(45.0, 24.0, 155.0, 15.0);
        [self.contentView bringSubviewToFront:dateLabel];
    }
    
    if (self.countLabel) {
        self.countLabel.frame = CGRectMake(10.0, 7.0, 280.0, 30.0);
        [self.contentView bringSubviewToFront:countLabel];
    }
}

- (void)bindDay:(NSDay *)day;
{
    if (day == nil)
        return;
    
    self.day = day;
    
    UIImage *picture = [UIImage imageNamed:@"picture.png"];
    [self.pictureButton setImage:picture forState:UIControlStateNormal];
    
    self.titleLabel.text = self.day.title;
    self.dateLabel.text = [self.day dateToString];
    self.countLabel.text = self.day.daysCountText;
}

- (void)pictureButtonTapped
{
    if(self.day) {
        [self.delegate tableViewCell:self
            imageButtonTappedWithDay:self.day];
    }
}

@end
